$(function () {
    let buttonGroup = [
        {
            id: 'btn_search',
            size: 'small',
            text: '查询',
            iconCls: 'icon-search',
            onClick: btn_search,
            plain: true
        },
        // {
        //     id: 'btn_add',
        //     size: 'small',
        //     text: '生成发货单',
        //     iconCls: 'icon-add',
        //     plain: true,
        //     onClick: btn_add
        // },
        // {
        //     id: 'btn_print_bill',
        //     size: 'small',
        //     text: '打印发货单',
        //     iconCls: 'icon-print',
        //     plain: true,
        //     onClick: btn_print_bill
        // },
        // {
        //     id: 'btn_delete',
        //     size: 'small',
        //     text: '作废发货单',
        //     iconCls: 'icon-print',
        //     plain: true,
        //     onClick: btn_delete
        // },
        {
            id: 'btn_export',
            size: 'small',
            text: '导出Excel',
            iconCls: 'icon-page_excel',
            plain: true,
            onClick: btn_export_excel
        },
        {
            id: 'btn_cancel_filter',
            size: 'small',
            text: '清除过滤',
            iconCls: 'icon-clear',
            plain: true,
            onClick: btn_cancel_filter
        },
        // {
        //     id: 'btn_print',
        //     size: 'small',
        //     text: '打印细码单',
        //     iconCls: 'icon-print',
        //     plain: true,
        //     onClick: btn_print
        // },
        {
            id: 'btn_grid_columns',
            size: 'small',
            text: '列',
            iconCls: 'icon-text_columns',
            plain: true,
            onClick: btn_open_column_list
        },
        // {
        //     id: 'btn_cancel_audit',
        //     size: 'small',
        //     disabled: true,
        //     text: '消审',
        //     iconCls: 'icon-exclamation',
        //     onClick: btn_cancel_audit,
        //     plain: true
        // },
        {
            id: 'btn_close', size: 'small', text: '关闭', iconCls: 'icon-no', onClick: btn_close,
            plain: true
        },
    ];
    buttonGen($('#north'), buttonGroup);
    bindEnterEvents();

})


//  选中事件
function onSelect(title, index) {
    function state(f) {
        if (f) {
            $('#start').datebox('disable');
            $('#end').datebox('disable');
        } else {
            $('#start').datebox('enable');
            $('#end').datebox('enable');
        }
    }

    switch (index) {
        case 0:
            if ($('#v_grid_sum').data('kendoGrid') == undefined) {
                grid_sum();
            }
            if ($('#v_grid_detail').data('kendoGrid') == undefined) {
                grid_detail();
            }
            state(false);
            getgriddata(0);
            $('#btn_add').linkbutton('enable');
            $('#btn_delete').linkbutton('disable');
            $('#btn_print_bill').linkbutton('disable');
            $('#btn_print').linkbutton('enable');
            // setgrid();

            break;
        case 1:
            if ($('#v_grid_2').data('kendoGrid') == undefined) {
                grid_2();
            }
            state(false);
            getgriddata(2);
            $('#btn_add').linkbutton('disable');
            $('#btn_delete').linkbutton('enable');
            $('#btn_print_bill').linkbutton('enable');
            $('#btn_print').linkbutton('disable');
            break;
        case 2:
            if ($('#v_grid_sum_grammage').data('kendoGrid') == undefined) {
                grid_sum_grammage();
            }
            if ($('#v_grid_detail_grammage').data('kendoGrid') == undefined) {
                grid_detail_grammage();
            }
            state(false);
            getgriddata(3);
            $('#btn_add').linkbutton('enable');
            $('#btn_delete').linkbutton('disable');
            $('#btn_print_bill').linkbutton('disable');
            $('#btn_print').linkbutton('enable');
            // setgrid();

            break;
    }
}

//报表数据源
function getgriddata(val) {
    var kGrid = getSelectedGrid();
    $.messager.progress({title: '', msg: '请稍等...', interval: 5000});
    $.ajax({
        url: "customize/callProc",
        type: 'post',
        data: JSON.stringify(['usp_cp_dailyproduction',$('#start').datebox('getValue'),$('#end').datebox('getValue'),val]),
        contentType: "application/json;charset=UTF-8",
        success: function (cdata) {
            let  data=cdata.data;
            console.log(data);
            kGrid.dataSource.data(data);
            if (val==0 && data.length>0){
                // getgriddata_detail(data[0]);
                getgriddata_detail_all()
            }
        }, complete: function () {
            $.messager.progress('close');
        }

    })
}

function getgriddata_detail(dataItem) {
    var kGrid = $('#v_grid_detail').data('kendoGrid');
    $.messager.progress({title: '', msg: '请稍等...', interval: 5000});
    $.ajax({
        url: "customize/callProc",
        type: 'post',
        data: JSON.stringify(['usp_cp_dailyproduction',dataItem.B_Date,dataItem.B_Date,1]),
        contentType: "application/json;charset=UTF-8",
        success: function (cdata) {
            let  data=cdata.data;
            console.log(data);
            kGrid.dataSource.data(data);
        }, complete: function () {
            $.messager.progress('close');
        }

    })
}

function getgriddata_detail_grammage(dataItem) {
    var kGrid = $('#v_grid_detail_grammage').data('kendoGrid');
    $.messager.progress({title: '', msg: '请稍等...', interval: 5000});
    $.ajax({
        url: "customize/callProc",
        type: 'post',
        data: JSON.stringify(['usp_cp_dailyproduction',dataItem.B_Date,dataItem.B_Date,4]),
        contentType: "application/json;charset=UTF-8",
        success: function (cdata) {
            let  data=cdata.data;
            console.log(data);
            kGrid.dataSource.data(data);
        }, complete: function () {
            $.messager.progress('close');
        }

    })
}

function getgriddata_detail_all() {
    var kGrid = $('#v_grid_detail').data('kendoGrid');
    $.messager.progress({title: '', msg: '请稍等...', interval: 5000});
    $.ajax({
        url: "customize/callProc",
        type: 'post',
        data: JSON.stringify(['usp_cp_dailyproduction',$('#start').datebox('getValue'),$('#end').datebox('getValue'),1]),
        contentType: "application/json;charset=UTF-8",
        success: function (cdata) {
            let  data=cdata.data;
            console.log(data);
            kGrid.dataSource.data(data);
        }, complete: function () {
            $.messager.progress('close');
        }

    })
}

function getgriddata_detail_all_grammage() {
    var kGrid = $('#v_grid_detail_grammage').data('kendoGrid');
    $.messager.progress({title: '', msg: '请稍等...', interval: 5000});
    $.ajax({
        url: "customize/callProc",
        type: 'post',
        data: JSON.stringify(['usp_cp_dailyproduction',$('#start').datebox('getValue'),$('#end').datebox('getValue'),4]),
        contentType: "application/json;charset=UTF-8",
        success: function (cdata) {
            let  data=cdata.data;
            console.log(data);
            kGrid.dataSource.data(data);
        }, complete: function () {
            $.messager.progress('close');
        }

    })
}

/**
 * 2021年8月11日 11:06:32 chanchaw
 * 第一个标签页 - 日汇总和明细
 */
function grid_sum() {
    let SUFFIX = "_a";
    let B_Date = new kendo.data.DataSource({data: []});
    let dataSource = new kendo.data.DataSource({
        schema: {
            model: {
                fields: {
                    pssum: {type: "number"},
                    qtysum: {type: "number"},
                }
            }
        },
        aggregate: [
            {field: "pssum", aggregate: "sum"},
            {field: "qtysum", aggregate: "sum"},
        ],
        change: function (e) {
            // let data = this.data();
            //获取过滤条件
            let filtrer = this.filter();
            //获取所有数据
            let allData = this.data();
            //进行过滤条件的筛选
            let query = new kendo.data.Query(allData);
            //获取过过滤后的数据
            let data = query.filter(filtrer).data;

            B_Date.data(unique(data, 'B_Date'));

        },
        // pageSize: 200,
        requestStart: function (e) {
            $('.easyui-linkbutton').each(function (index, item) {
                let btn = $(item);
                if (btn.linkbutton('options').text === '查询' || (btn.linkbutton('options').text === '过滤')) {
                    btn.linkbutton('disable')
                }
            })
        },
        requestEnd: function (e) {
            let response = e.response;
            if (response && response['message']) {
                $.messager.show({title: '提示', msg: response['message']})
            }
            $('.easyui-linkbutton').each(function (index, item) {
                let btn = $(item);
                if (btn.linkbutton('options').text === '查询' || (btn.linkbutton('options').text === '过滤')) {
                    btn.linkbutton('enable')
                }
            })
        }
    });
    let columns = [
        {field: "no", title: "#", filterable: false, template: "<span class='row_number'></span>", width: 35},
        {
            field: "B_Date",
            title: '日期',
            width: 110,
            attributes: {'class': 'kendo-custum-char'},
            headerAttributes: {'style': 'text-align:center;'},
            footerAttributes: {'style': 'color: red;text-align:center;'},
            filterable: false
            // filterable: {checkAll: true, multi: true, search: true, dataSource: B_Date}
        },

        {
            field: "pssum",
            title: '匹数',
            width: 60,
            template: item => item['pssum'] ? parseFloat(item['pssum']).toFixed(0) : '',
            footerTemplate: '#=kendo.format("{0:n0}",sum)#',
            footerAttributes: {'style': 'text-align:right;'},
            attributes: {'class': 'kendo-custum-number'},
            headerAttributes: {'style': 'text-align:center;'},
            filterable: false
        },
        {
            field: "qtysum",
            title: '重量',
            width: 90,
            template: item => item['qtysum'] ? parseFloat(item['qtysum']).toFixed(1) : '',
            footerTemplate: '#=kendo.format("{0:n1}",sum)#',
            footerAttributes: {'style': 'text-align:right;'},
            attributes: {'class': 'kendo-custum-number'},
            headerAttributes: {'style': 'text-align:center;'},
            filterable: false
        },
    ];
    let grid = $("#v_grid_sum").kendoGrid({
        sortable: true,
        resizable: true,
        selectable: "multiple row",
        editable: false,
        filterable: {
            extra: false
        },
        dataSource: dataSource,
        columns:columns,
        columnMenu: false,
        excel: {
            fileName: "日产量汇总.xlsx",
            proxyURL: "",
            allPages: true,
            filterable: true
        },
        height: '99%',
        // pageable: {
        //     pageSize: 200
        // },
        reorderable: true,
        dataBound: function () {
            let rows = this.items();
            let kGrid = $('#v_grid_sum').data('kendoGrid');
            $(rows).each(function () {
                let index = $(this).index() + 1;
                let rowLabel = $(this).find(".row_number");
                $(rowLabel).html(index);
                let dataItem = kGrid.dataItem($(this));

            });
        },
        columnHide: function (e) {
            //列隐藏事件
            let field = e.column.field;
            columnResizeEvent($('body').data('menu_id'), field, null, true, null, SUFFIX);
        },
        columnShow: function (e) {
            //列显示事件
            let field = e.column.field;
            columnResizeEvent($('body').data('menu_id'), field, null, false, null, SUFFIX);
        },
        columnResize: function (e) {
            //  列伸缩事件
            let field = e.column.field;
            let newWidth = e.newWidth;
            if (newWidth < 20) {
                columnResizeEvent($('body').data('menu_id'), field, null, true, null, SUFFIX);
            } else {
                columnResizeEvent($('body').data('menu_id'), field, newWidth, null, null, SUFFIX);
            }
        },
        columnReorder: function (e) {
            //  列拖动事件
            let newColumns = JSON.parse(JSON.stringify(e.sender.columns))
            newColumns = reorder(newColumns, e.oldIndex, e.newIndex);
            let menuId = $('body').data('menu_id');
            let col = []
            for (let i = 0; i < newColumns.length; i++) {
                if (newColumns[i]['field'])
                    col.push({
                        serialNum: i,
                        sysMenuId: menuId,
                        field: newColumns[i]['field'] + SUFFIX
                    });
            }
            $.ajax({
                url: 'sysUserTableConfig/modify/reorder',
                contentType: 'application/json',
                data: JSON.stringify(col),
                type: 'post',
                dataType: 'json',
                success: function (data) {
                }
            })
        },
    });
    let grid01 = $('#v_grid_sum').data('kendoGrid');
    // loadOptions(grid01, columns, SUFFIX, $('body').data('menu_id'));
    // grid01.setOptions({
    //     columns: columns
    // });
    grid.on('dblclick', 'tr', function (e) {
        // setgrid_fh();
        // //  修改单据
        // let kGrid = $("#v_grid_sum").data("kendoGrid");
        // let dataItem = kGrid.dataItem(e.currentTarget);
        // open_fhdetail(dataItem);
    })
    grid.on('click', 'tr', function (e) {
        let kGrid = $("#v_grid_sum").data("kendoGrid");
        let dataItem = kGrid.dataItem(e.currentTarget);
        console.log(dataItem);
        getgriddata_detail(dataItem)
        // if (dataItem['is_audit']) {
        //     $('#btn_cancel_audit').linkbutton('enable')
        // } else {
        //     $('#btn_cancel_audit').linkbutton('disable')
        // }
    })

    //右击事件
    grid.on('contextmenu', 'tr', function (e) {
        e.preventDefault();
        var div = "<div id='menu'>"
            + "<div>取消选中</div>"
            + "</div>";
        if ($('#menu').length <= 0) {
            $('body').append(div);
            $('#menu').menu({
                onClick: function (item) {
                    var name = item.text;
                    switch (name) {
                        case '取消选中':
                            let kGrid = $("#v_grid_sum").data("kendoGrid");
                            kGrid.clearSelection();
                            getgriddata_detail_all()
                            break;
                    }
                }
            });
        }
        $('#menu').menu('show', {
            left: e.pageX,
            top: e.pageY
        });
    });
}

function grid_detail() {
    let SUFFIX = "_detail";
    let plan_code = new kendo.data.DataSource({data: []});
    let order_code = new kendo.data.DataSource({data: []});
    let client_code = new kendo.data.DataSource({data: []});
    let product_name = new kendo.data.DataSource({data: []});
    let menfu = new kendo.data.DataSource({data: []});
    let color = new kendo.data.DataSource({data: []});
    let company_name = new kendo.data.DataSource({data: []});


    let dataSource = new kendo.data.DataSource({
        schema: {
            model: {
                fields: {
                    pssum: {type: "number"},
                    qtysum: {type: "number"},
                }
            }
        },
        aggregate: [
            {field: "pssum", aggregate: "sum"},
            {field: "qtysum", aggregate: "sum"},
        ],
        change: function (e) {
            // let data = this.data();
            //获取过滤条件
            let filtrer = this.filter();
            //获取所有数据
            let allData = this.data();
            //进行过滤条件的筛选
            let query = new kendo.data.Query(allData);
            //获取过过滤后的数据
            let data = query.filter(filtrer).data;

            // company_name.data(unique(data, 'company_name'));

        },
        // pageSize: 200,
        requestStart: function (e) {
            $('.easyui-linkbutton').each(function (index, item) {
                let btn = $(item);
                if (btn.linkbutton('options').text === '查询' || (btn.linkbutton('options').text === '过滤')) {
                    btn.linkbutton('disable')
                }
            })
        },
        requestEnd: function (e) {
            let response = e.response;
            if (response && response['message']) {
                $.messager.show({title: '提示', msg: response['message']})
            }
            $('.easyui-linkbutton').each(function (index, item) {
                let btn = $(item);
                if (btn.linkbutton('options').text === '查询' || (btn.linkbutton('options').text === '过滤')) {
                    btn.linkbutton('enable')
                }
            })
        }
    });
    let columns = [
        {field: "no", title: "#", filterable: false, template: "<span class='row_number'></span>", width: 35},
        // {
        //     field: "company_name",
        //     title: '客户',
        //     width: 110,
        //     attributes: {'class': 'kendo-custum-char'},
        //     headerAttributes: {'style': 'text-align:center;'},
        //     footerAttributes: {'style': 'color: red;text-align:center;'},
        //     filterable: false
        //     // filterable: {checkAll: true, multi: true, search: true, dataSource: B_Date}
        // },
        {
            field: "plan_code",
            title: '机台卡片',
            width: 110,
            attributes: {'class': 'kendo-custum-char'},
            headerAttributes: {'style': 'text-align:center;'},
            footerAttributes: {'style': 'color: red;text-align:center;'},
            filterable: false
            // filterable: {checkAll: true, multi: true, search: true, dataSource: B_Date}
        },
        {
            field: "B_JTH",
            title: '机台号',
            width: 110,
            attributes: {'class': 'kendo-custum-char'},
            headerAttributes: {'style': 'text-align:center;'},
            footerAttributes: {'style': 'color: red;text-align:center;'},
            filterable: false
            // filterable: {checkAll: true, multi: true, search: true, dataSource: B_Date}
        },
        {
            field: "order_code",
            title: '订单号',
            width: 110,
            attributes: {'class': 'kendo-custum-char'},
            headerAttributes: {'style': 'text-align:center;'},
            footerAttributes: {'style': 'color: red;text-align:center;'},
            filterable: false
            // filterable: {checkAll: true, multi: true, search: true, dataSource: B_Date}
        },
        {
            field: "client_code",
            title: '客户单号',
            width: 110,
            attributes: {'class': 'kendo-custum-char'},
            headerAttributes: {'style': 'text-align:center;'},
            footerAttributes: {'style': 'color: red;text-align:center;'},
            filterable: false
            // filterable: {checkAll: true, multi: true, search: true, dataSource: B_Date}
        },
        {
            field: "product_name",
            title: '品名',
            width: 110,
            attributes: {'class': 'kendo-custum-char'},
            headerAttributes: {'style': 'text-align:center;'},
            footerAttributes: {'style': 'color: red;text-align:center;'},
            filterable: false
            // filterable: {checkAll: true, multi: true, search: true, dataSource: B_Date}
        },
        {
            field: "menfu",
            title: '规格',
            width: 110,
            attributes: {'class': 'kendo-custum-char'},
            headerAttributes: {'style': 'text-align:center;'},
            footerAttributes: {'style': 'color: red;text-align:center;'},
            filterable: false
            // filterable: {checkAll: true, multi: true, search: true, dataSource: B_Date}
        },
        {
            field: "color",
            title: '颜色',
            width: 110,
            attributes: {'class': 'kendo-custum-char'},
            headerAttributes: {'style': 'text-align:center;'},
            footerAttributes: {'style': 'color: red;text-align:center;'},
            filterable: false
            // filterable: {checkAll: true, multi: true, search: true, dataSource: B_Date}
        },
        {
            field: "pssum",
            title: '匹数',
            width: 60,
            template: item => item['pssum'] ? parseFloat(item['pssum']).toFixed(0) : '',
            footerTemplate: '#=kendo.format("{0:n0}",sum)#',
            footerAttributes: {'style': 'text-align:right;'},
            attributes: {'class': 'kendo-custum-number'},
            headerAttributes: {'style': 'text-align:center;'},
            filterable: false
        },
        {
            field: "qtysum",
            title: '重量',
            width: 90,
            template: item => item['qtysum'] ? parseFloat(item['qtysum']).toFixed(1) : '',
            footerTemplate: '#=kendo.format("{0:n1}",sum)#',
            footerAttributes: {'style': 'text-align:right;'},
            attributes: {'class': 'kendo-custum-number'},
            headerAttributes: {'style': 'text-align:center;'},
            filterable: false
        },
        {
            field: "memo",
            title: ' ',
            width: 50,
            filterable: false
        },
    ];
    let grid = $("#v_grid_detail").kendoGrid({
        sortable: true,
        resizable: true,
        selectable: "multiple row",
        editable: false,
        filterable: {
            extra: false
        },
        dataSource: dataSource,
        // columns:columns,
        columnMenu: false,
        excel: {
            fileName: "日产量明细.xlsx",
            proxyURL: "",
            allPages: true,
            filterable: true
        },
        height: '99%',
        // pageable: {
        //     pageSize: 200
        // },
        reorderable: true,
        dataBound: function () {
            let rows = this.items();
            let kGrid = $('#v_grid_detail').data('kendoGrid');
            $(rows).each(function () {
                let index = $(this).index() + 1;
                let rowLabel = $(this).find(".row_number");
                $(rowLabel).html(index);
                let dataItem = kGrid.dataItem($(this));

            });
        },
        columnHide: function (e) {
            //列隐藏事件
            let field = e.column.field;
            columnResizeEvent($('body').data('menu_id'), field, null, true, null, SUFFIX);
        },
        columnShow: function (e) {
            //列显示事件
            let field = e.column.field;
            columnResizeEvent($('body').data('menu_id'), field, null, false, null, SUFFIX);
        },
        columnResize: function (e) {
            //  列伸缩事件
            let field = e.column.field;
            let newWidth = e.newWidth;
            if (newWidth < 20) {
                columnResizeEvent($('body').data('menu_id'), field, null, true, null, SUFFIX);
            } else {
                columnResizeEvent($('body').data('menu_id'), field, newWidth, null, null, SUFFIX);
            }
        },
        columnReorder: function (e) {
            //  列拖动事件
            let newColumns = JSON.parse(JSON.stringify(e.sender.columns))
            newColumns = reorder(newColumns, e.oldIndex, e.newIndex);
            let menuId = $('body').data('menu_id');
            let col = []
            for (let i = 0; i < newColumns.length; i++) {
                if (newColumns[i]['field'])
                    col.push({
                        serialNum: i,
                        sysMenuId: menuId,
                        field: newColumns[i]['field'] + SUFFIX
                    });
            }
            $.ajax({
                url: 'sysUserTableConfig/modify/reorder',
                contentType: 'application/json',
                data: JSON.stringify(col),
                type: 'post',
                dataType: 'json',
                success: function (data) {
                }
            })
        },
    });
    let grid01 = $('#v_grid_detail').data('kendoGrid');
    loadOptions(grid01, columns, SUFFIX, $('body').data('menu_id'));
    grid01.setOptions({
        columns: columns
    });
    grid.on('dblclick', 'tr', function (e) {
        // setgrid_fh();
        // //  修改单据
        // let kGrid = $("#v_grid_detail").data("kendoGrid");
        // let dataItem = kGrid.dataItem(e.currentTarget);
        // open_fhdetail(dataItem);
    })
    grid.on('click', 'tr', function (e) {
        let kGrid = $("#v_grid_detail").data("kendoGrid");
        let dataItem = kGrid.dataItem(e.currentTarget);
        console.log(dataItem);
        // if (dataItem['is_audit']) {
        //     $('#btn_cancel_audit').linkbutton('enable')
        // } else {
        //     $('#btn_cancel_audit').linkbutton('disable')
        // }
    })
}


/**
 * 2021年8月11日 11:06:32 chanchaw
 * 成品管理 - 第二个标签页 - 待处理
 */
function grid_2() {
    let SUFFIX = "_dcl";
    let plan_code = new kendo.data.DataSource({data: []});
    let order_code = new kendo.data.DataSource({data: []});
    let client_code = new kendo.data.DataSource({data: []});
    let product_name = new kendo.data.DataSource({data: []});
    let menfu = new kendo.data.DataSource({data: []});
    let color = new kendo.data.DataSource({data: []});
    let company_name = new kendo.data.DataSource({data: []});
    let B_CiPinReason = new kendo.data.DataSource({data: []});
    let B_JTH = new kendo.data.DataSource({data: []});


    let dataSource = new kendo.data.DataSource({
        schema: {
            model: {
                fields: {
                    ps: {type: "number"},
                    qty: {type: "number"},
                    meter: {type: "number"},
                }
            }
        },
        aggregate: [
            {field: "ps", aggregate: "sum"},
            {field: "qty", aggregate: "sum"},
            {field: "meter", aggregate: "sum"},
        ],
        change: function (e) {
            // let data = this.data();
            //获取过滤条件
            let filtrer = this.filter();
            //获取所有数据
            let allData = this.data();
            //进行过滤条件的筛选
            let query = new kendo.data.Query(allData);
            //获取过过滤后的数据
            let data = query.filter(filtrer).data;

            plan_code.data(unique(data, 'plan_code'));
            order_code.data(unique(data, 'order_code'));
            client_code.data(unique(data, 'client_code'));
            product_name.data(unique(data, 'product_name'));
            menfu.data(unique(data, 'menfu'));
            color.data(unique(data, 'color'));
            B_CiPinReason.data(unique(data, 'B_CiPinReason'));
            B_JTH.data(unique(data, 'B_JTH'));

        },
        // pageSize: 200,
        requestStart: function (e) {
            $('.easyui-linkbutton').each(function (index, item) {
                let btn = $(item);
                if (btn.linkbutton('options').text === '查询' || (btn.linkbutton('options').text === '过滤')) {
                    btn.linkbutton('disable')
                }
            })
        },
        requestEnd: function (e) {
            let response = e.response;
            if (response && response['message']) {
                $.messager.show({title: '提示', msg: response['message']})
            }
            $('.easyui-linkbutton').each(function (index, item) {
                let btn = $(item);
                if (btn.linkbutton('options').text === '查询' || (btn.linkbutton('options').text === '过滤')) {
                    btn.linkbutton('enable')
                }
            })
        }
    });
    let columns = [
        {field: "no", title: "#", filterable: false, template: "<span class='row_number'></span>", width: 35},
        // {
        //     field: "company_name",
        //     title: '客户',
        //     width: 110,
        //     attributes: {'class': 'kendo-custum-char'},
        //     headerAttributes: {'style': 'text-align:center;'},
        //     footerAttributes: {'style': 'color: red;text-align:center;'},
        //     filterable: false
        //     // filterable: {checkAll: true, multi: true, search: true, dataSource: B_Date}
        // },
        {
            field: "plan_code",
            title: '机台卡片',
            width: 80,
            attributes: {'class': 'kendo-custum-char'},
            headerAttributes: {'style': 'text-align:center;'},
            footerAttributes: {'style': 'color: red;text-align:center;'},
            // filterable: false
            filterable: {checkAll: true, multi: true, search: true, dataSource: plan_code}
        },
        {
            field: "order_code",
            title: '订单号',
            width: 90,
            attributes: {'class': 'kendo-custum-char'},
            headerAttributes: {'style': 'text-align:center;'},
            footerAttributes: {'style': 'color: red;text-align:center;'},
            // filterable: false
            filterable: {checkAll: true, multi: true, search: true, dataSource: order_code}
        },
        {
            field: "client_code",
            title: '客户单号',
            width: 110,
            attributes: {'class': 'kendo-custum-char'},
            headerAttributes: {'style': 'text-align:center;'},
            footerAttributes: {'style': 'color: red;text-align:center;'},
            // filterable: false
            filterable: {checkAll: true, multi: true, search: true, dataSource: client_code}
        },
        {
            field: "product_name",
            title: '品名',
            width: 110,
            attributes: {'class': 'kendo-custum-char'},
            headerAttributes: {'style': 'text-align:center;'},
            footerAttributes: {'style': 'color: red;text-align:center;'},
            // filterable: false
            filterable: {checkAll: true, multi: true, search: true, dataSource: product_name}
        },
        {
            field: "menfu",
            title: '规格',
            width: 110,
            attributes: {'class': 'kendo-custum-char'},
            headerAttributes: {'style': 'text-align:center;'},
            footerAttributes: {'style': 'color: red;text-align:center;'},
            // filterable: false
            filterable: {checkAll: true, multi: true, search: true, dataSource: menfu}
        },
        {
            field: "color",
            title: '颜色',
            width: 80,
            attributes: {'class': 'kendo-custum-char'},
            headerAttributes: {'style': 'text-align:center;'},
            footerAttributes: {'style': 'color: red;text-align:center;'},
            // filterable: false
            filterable: {checkAll: true, multi: true, search: true, dataSource: color}
        },
        {
            field: "B_JTH",
            title: '机台号',
            width: 70,
            attributes: {'class': 'kendo-custum-char'},
            headerAttributes: {'style': 'text-align:center;'},
            footerAttributes: {'style': 'color: red;text-align:center;'},
            // filterable: false
            filterable: {checkAll: true, multi: true, search: true, dataSource: B_JTH}
        },
        {
            field: "ps",
            title: '匹数',
            width: 60,
            template: item => item['ps'] ? parseFloat(item['ps']).toFixed(0) : '',
            footerTemplate: '#=kendo.format("{0:n0}",sum)#',
            footerAttributes: {'style': 'text-align:right;'},
            attributes: {'class': 'kendo-custum-number'},
            headerAttributes: {'style': 'text-align:center;'},
            filterable: false
        },
        {
            field: "qty",
            title: '重量',
            width: 90,
            template: item => item['qty'] ? parseFloat(item['qty']).toFixed(1) : '',
            footerTemplate: '#=kendo.format("{0:n1}",sum)#',
            footerAttributes: {'style': 'text-align:right;'},
            attributes: {'class': 'kendo-custum-number'},
            headerAttributes: {'style': 'text-align:center;'},
            filterable: false
        },
        {
            field: "meter",
            title: '米数',
            width: 90,
            template: item => item['meter'] ? parseFloat(item['meter']).toFixed(1) : '',
            footerTemplate: '#=kendo.format("{0:n1}",sum)#',
            footerAttributes: {'style': 'text-align:right;'},
            attributes: {'class': 'kendo-custum-number'},
            headerAttributes: {'style': 'text-align:center;'},
            filterable: false
        },
        {
            field: "B_CiPinReason",
            title: '待处理原因',
            width: 110,
            attributes: {'class': 'kendo-custum-char'},
            headerAttributes: {'style': 'text-align:center;'},
            footerAttributes: {'style': 'color: red;text-align:center;'},
            // filterable: false
            filterable: {checkAll: true, multi: true, search: true, dataSource: B_CiPinReason}
        },
        {
            field: "B_Date",
            title: '打卷时间',
            width: 150,
            attributes: {'class': 'kendo-custum-char'},
            headerAttributes: {'style': 'text-align:center;'},
            footerAttributes: {'style': 'color: red;text-align:center;'},
            filterable: false
            // filterable: {checkAll: true, multi: true, search: true, dataSource: B_JTH}
        },
        {
            field: "memo",
            title: ' ',
            width: 50,
            filterable: false
        },
    ];
    let grid = $("#v_grid_2").kendoGrid({
        sortable: true,
        resizable: true,
        selectable: "multiple row",
        editable: false,
        filterable: {
            extra: false
        },
        dataSource: dataSource,
        // columns:columns,
        columnMenu: false,
        excel: {
            fileName: "待处理明细.xlsx",
            proxyURL: "",
            allPages: true,
            filterable: true
        },
        height: '99%',
        // pageable: {
        //     pageSize: 200
        // },
        reorderable: true,
        dataBound: function () {
            let rows = this.items();
            let kGrid = $('#v_grid_2').data('kendoGrid');
            $(rows).each(function () {
                let index = $(this).index() + 1;
                let rowLabel = $(this).find(".row_number");
                $(rowLabel).html(index);
                let dataItem = kGrid.dataItem($(this));

            });
        },
        columnHide: function (e) {
            //列隐藏事件
            let field = e.column.field;
            columnResizeEvent($('body').data('menu_id'), field, null, true, null, SUFFIX);
        },
        columnShow: function (e) {
            //列显示事件
            let field = e.column.field;
            columnResizeEvent($('body').data('menu_id'), field, null, false, null, SUFFIX);
        },
        columnResize: function (e) {
            //  列伸缩事件
            let field = e.column.field;
            let newWidth = e.newWidth;
            if (newWidth < 20) {
                columnResizeEvent($('body').data('menu_id'), field, null, true, null, SUFFIX);
            } else {
                columnResizeEvent($('body').data('menu_id'), field, newWidth, null, null, SUFFIX);
            }
        },
        columnReorder: function (e) {
            //  列拖动事件
            let newColumns = JSON.parse(JSON.stringify(e.sender.columns))
            newColumns = reorder(newColumns, e.oldIndex, e.newIndex);
            let menuId = $('body').data('menu_id');
            let col = []
            for (let i = 0; i < newColumns.length; i++) {
                if (newColumns[i]['field'])
                    col.push({
                        serialNum: i,
                        sysMenuId: menuId,
                        field: newColumns[i]['field'] + SUFFIX
                    });
            }
            $.ajax({
                url: 'sysUserTableConfig/modify/reorder',
                contentType: 'application/json',
                data: JSON.stringify(col),
                type: 'post',
                dataType: 'json',
                success: function (data) {
                }
            })
        },
    });
    let grid01 = $('#v_grid_2').data('kendoGrid');
    loadOptions(grid01, columns, SUFFIX, $('body').data('menu_id'));
    grid01.setOptions({
        columns: columns
    });
    grid.on('dblclick', 'tr', function (e) {
        // setgrid_fh();
        // //  修改单据
        // let kGrid = $("#v_grid_2").data("kendoGrid");
        // let dataItem = kGrid.dataItem(e.currentTarget);
        // open_fhdetail(dataItem);
    })
    grid.on('click', 'tr', function (e) {
        let kGrid = $("#v_grid_2").data("kendoGrid");
        let dataItem = kGrid.dataItem(e.currentTarget);
        // console.log(dataItem);
        // if (dataItem['is_audit']) {
        //     $('#btn_cancel_audit').linkbutton('enable')
        // } else {
        //     $('#btn_cancel_audit').linkbutton('disable')
        // }
    })
    //右击事件
    grid.on('contextmenu', 'tr', function (e) {
        e.preventDefault();
        let kGrid = $("#v_grid_2").data("kendoGrid");
        kGrid.clearSelection();
        kGrid.select(this);
        let row = kGrid.select();
        let dataItem = kGrid.dataItem(row);
        let div = "<div id = 'mx_gridmenu'>"
            + "<div data-options=\"iconCls:'icon-edit'\">转正品</div>"
            + "</div>";
        if ($('#mx_gridmenu').length <= 0)
            $('body').append(div);
        $('#mx_gridmenu').menu({
            onClick: function (item) {
                let name = item.text;
                switch (name) {
                    case '转正品':
                        $.messager.confirm('确认', '确定当前选中行转正品吗？', function (r) {
                            if (r) {
                                $.ajax({
                                    url: 'gjrkbillCp/updateEDPById',
                                    contentType: 'application/json',
                                    data: JSON.stringify({bItemID:dataItem.B_ItemID,bEDP:'001'}),
                                    type: 'post',
                                    dataType: 'json',
                                    success: function (data) {
                                        console.log(data);
                                        btn_search()
                                        $.messager.show({title: '提示', msg: '成功'});
                                    }
                                })
                            }
                        })

                        break;
                }
            }
        });
        $('#mx_gridmenu').menu('show', {
            left: e.pageX,
            top: e.pageY
        });
    });
}

/**
 * 2025年5月29日
 * 第三个标签 - 克重表
 */
function grid_sum_grammage() {
    let SUFFIX = "_1a";
    let B_Date = new kendo.data.DataSource({data: []});
    let dataSource = new kendo.data.DataSource({
        schema: {
            model: {
                fields: {
                    pssum: {type: "number"},
                    qtysum: {type: "number"},
                }
            }
        },
        aggregate: [
            {field: "pssum", aggregate: "sum"},
            {field: "qtysum", aggregate: "sum"},
        ],
        change: function (e) {
            // let data = this.data();
            //获取过滤条件
            let filtrer = this.filter();
            //获取所有数据
            let allData = this.data();
            //进行过滤条件的筛选
            let query = new kendo.data.Query(allData);
            //获取过过滤后的数据
            let data = query.filter(filtrer).data;

            B_Date.data(unique(data, 'B_Date'));

        },
        // pageSize: 200,
        requestStart: function (e) {
            $('.easyui-linkbutton').each(function (index, item) {
                let btn = $(item);
                if (btn.linkbutton('options').text === '查询' || (btn.linkbutton('options').text === '过滤')) {
                    btn.linkbutton('disable')
                }
            })
        },
        requestEnd: function (e) {
            let response = e.response;
            if (response && response['message']) {
                $.messager.show({title: '提示', msg: response['message']})
            }
            $('.easyui-linkbutton').each(function (index, item) {
                let btn = $(item);
                if (btn.linkbutton('options').text === '查询' || (btn.linkbutton('options').text === '过滤')) {
                    btn.linkbutton('enable')
                }
            })
        }
    });
    let columns = [
        {field: "no", title: "#", filterable: false, template: "<span class='row_number'></span>", width: 35},
        {
            field: "B_Date",
            title: '日期',
            width: 110,
            attributes: {'class': 'kendo-custum-char'},
            headerAttributes: {'style': 'text-align:center;'},
            footerAttributes: {'style': 'color: red;text-align:center;'},
            filterable: false
            // filterable: {checkAll: true, multi: true, search: true, dataSource: B_Date}
        },

        {
            field: "pssum",
            title: '匹数',
            width: 60,
            template: item => item['pssum'] ? parseFloat(item['pssum']).toFixed(0) : '',
            footerTemplate: '#=kendo.format("{0:n0}",sum)#',
            footerAttributes: {'style': 'text-align:right;'},
            attributes: {'class': 'kendo-custum-number'},
            headerAttributes: {'style': 'text-align:center;'},
            filterable: false
        },
        {
            field: "qtysum",
            title: '重量',
            width: 90,
            template: item => item['qtysum'] ? parseFloat(item['qtysum']).toFixed(1) : '',
            footerTemplate: '#=kendo.format("{0:n1}",sum)#',
            footerAttributes: {'style': 'text-align:right;'},
            attributes: {'class': 'kendo-custum-number'},
            headerAttributes: {'style': 'text-align:center;'},
            filterable: false
        },
    ];
    let grid = $("#v_grid_sum_grammage").kendoGrid({
        sortable: true,
        resizable: true,
        selectable: "multiple row",
        editable: false,
        filterable: {
            extra: false
        },
        dataSource: dataSource,
        columns:columns,
        columnMenu: false,
        excel: {
            fileName: "克重汇总表.xlsx",
            proxyURL: "",
            allPages: true,
            filterable: true
        },
        height: '99%',
        // pageable: {
        //     pageSize: 200
        // },
        reorderable: true,
        dataBound: function () {
            let rows = this.items();
            let kGrid = $('#v_grid_sum_grammage').data('kendoGrid');
            $(rows).each(function () {
                let index = $(this).index() + 1;
                let rowLabel = $(this).find(".row_number");
                $(rowLabel).html(index);
                let dataItem = kGrid.dataItem($(this));

            });
        },
        columnHide: function (e) {
            //列隐藏事件
            let field = e.column.field;
            columnResizeEvent($('body').data('menu_id'), field, null, true, null, SUFFIX);
        },
        columnShow: function (e) {
            //列显示事件
            let field = e.column.field;
            columnResizeEvent($('body').data('menu_id'), field, null, false, null, SUFFIX);
        },
        columnResize: function (e) {
            //  列伸缩事件
            let field = e.column.field;
            let newWidth = e.newWidth;
            if (newWidth < 20) {
                columnResizeEvent($('body').data('menu_id'), field, null, true, null, SUFFIX);
            } else {
                columnResizeEvent($('body').data('menu_id'), field, newWidth, null, null, SUFFIX);
            }
        },
        columnReorder: function (e) {
            //  列拖动事件
            let newColumns = JSON.parse(JSON.stringify(e.sender.columns))
            newColumns = reorder(newColumns, e.oldIndex, e.newIndex);
            let menuId = $('body').data('menu_id');
            let col = []
            for (let i = 0; i < newColumns.length; i++) {
                if (newColumns[i]['field'])
                    col.push({
                        serialNum: i,
                        sysMenuId: menuId,
                        field: newColumns[i]['field'] + SUFFIX
                    });
            }
            $.ajax({
                url: 'sysUserTableConfig/modify/reorder',
                contentType: 'application/json',
                data: JSON.stringify(col),
                type: 'post',
                dataType: 'json',
                success: function (data) {
                }
            })
        },
    });
    let grid01 = $('#v_grid_sum_grammage').data('kendoGrid');
    // loadOptions(grid01, columns, SUFFIX, $('body').data('menu_id'));
    // grid01.setOptions({
    //     columns: columns
    // });
    grid.on('dblclick', 'tr', function (e) {
        // setgrid_fh();
        // //  修改单据
        // let kGrid = $("#v_grid_sum_grammage").data("kendoGrid");
        // let dataItem = kGrid.dataItem(e.currentTarget);
        // open_fhdetail(dataItem);
    })
    grid.on('click', 'tr', function (e) {
        let kGrid = $("#v_grid_sum_grammage").data("kendoGrid");
        let dataItem = kGrid.dataItem(e.currentTarget);
        console.log(dataItem);
        getgriddata_detail_grammage(dataItem)
        // if (dataItem['is_audit']) {
        //     $('#btn_cancel_audit').linkbutton('enable')
        // } else {
        //     $('#btn_cancel_audit').linkbutton('disable')
        // }
    })

    //右击事件
    grid.on('contextmenu', 'tr', function (e) {
        e.preventDefault();
        var div = "<div id='menu'>"
            + "<div>取消选中</div>"
            + "</div>";
        if ($('#menu').length <= 0) {
            $('body').append(div);
            $('#menu').menu({
                onClick: function (item) {
                    var name = item.text;
                    switch (name) {
                        case '取消选中':
                            let kGrid = $("#v_grid_sum_grammage").data("kendoGrid");
                            kGrid.clearSelection();
                            getgriddata_detail_all_grammage()
                            break;
                    }
                }
            });
        }
        $('#menu').menu('show', {
            left: e.pageX,
            top: e.pageY
        });
    });
}

function grid_detail_grammage() {
    let SUFFIX = "_detail";
    let plan_code = new kendo.data.DataSource({data: []});
    let B_JTH = new kendo.data.DataSource({data: []});
    let greyfabric_width = new kendo.data.DataSource({data: []});
    let product_name = new kendo.data.DataSource({data: []});
    let material_specification = new kendo.data.DataSource({data: []});
    let color = new kendo.data.DataSource({data: []});
    let isoverstep_kz = new kendo.data.DataSource({data: []});


    let dataSource = new kendo.data.DataSource({
        schema: {
            model: {
                fields: {
                    pssum: {type: "number"},
                    qtysum: {type: "number"},
                    mkz: {type: "number"},
                    B_MS: {type: "number"},
                }
            }
        },
        aggregate: [
            {field: "pssum", aggregate: "sum"},
            {field: "qtysum", aggregate: "sum"},
            {field: "mkz", aggregate: "sum"},
            {field: "B_MS", aggregate: "sum"},
        ],
        change: function (e) {
            // let data = this.data();
            //获取过滤条件
            let filtrer = this.filter();
            //获取所有数据
            let allData = this.data();
            //进行过滤条件的筛选
            let query = new kendo.data.Query(allData);
            //获取过过滤后的数据
            let data = query.filter(filtrer).data;

            plan_code.data(unique(data, 'plan_code'));
            B_JTH.data(unique(data, 'B_JTH'));
            greyfabric_width.data(unique(data, 'greyfabric_width'));
            material_specification.data(unique(data, 'material_specification'));
            product_name.data(unique(data, 'product_name'));
            color.data(unique(data, 'color'));
            isoverstep_kz.data(unique(data, 'isoverstep_kz'));

        },
        // pageSize: 200,
        requestStart: function (e) {
            $('.easyui-linkbutton').each(function (index, item) {
                let btn = $(item);
                if (btn.linkbutton('options').text === '查询' || (btn.linkbutton('options').text === '过滤')) {
                    btn.linkbutton('disable')
                }
            })
        },
        requestEnd: function (e) {
            let response = e.response;
            if (response && response['message']) {
                $.messager.show({title: '提示', msg: response['message']})
            }
            $('.easyui-linkbutton').each(function (index, item) {
                let btn = $(item);
                if (btn.linkbutton('options').text === '查询' || (btn.linkbutton('options').text === '过滤')) {
                    btn.linkbutton('enable')
                }
            })
        }
    });
    let columns = [
        {field: "no", title: "#", filterable: false, template: "<span class='row_number'></span>", width: 35},
        // {
        //     field: "company_name",
        //     title: '客户',
        //     width: 110,
        //     attributes: {'class': 'kendo-custum-char'},
        //     headerAttributes: {'style': 'text-align:center;'},
        //     footerAttributes: {'style': 'color: red;text-align:center;'},
        //     filterable: false
        //     // filterable: {checkAll: true, multi: true, search: true, dataSource: B_Date}
        // },
        {
            field: "plan_code",
            title: '机台卡片',
            width: 110,
            // attributes: {'class': 'kendo-custum-char'},
            attributes: {'style': 'white-space:nowrap;text-overflow:ellipsis;text-align:center;'},
            headerAttributes: {'style': 'text-align:center;'},
            footerAttributes: {'style': 'color: red;text-align:center;'},
            // filterable: false
            filterable: {checkAll: true, multi: true, search: true, dataSource: plan_code}
        },
        {
            field: "B_JTH",
            title: '机台号',
            width: 110,
            // attributes: {'class': 'kendo-custum-char'},
            attributes: {'style': 'white-space:nowrap;text-overflow:ellipsis;text-align:center;'},
            headerAttributes: {'style': 'text-align:center;'},
            footerAttributes: {'style': 'color: red;text-align:center;'},
            // filterable: false
            filterable: {checkAll: true, multi: true, search: true, dataSource: B_JTH}
        },
        // {
        //     field: "order_code",
        //     title: '订单号',
        //     width: 110,
        //     attributes: {'class': 'kendo-custum-char'},
        //     headerAttributes: {'style': 'text-align:center;'},
        //     footerAttributes: {'style': 'color: red;text-align:center;'},
        //     filterable: false
        //     // filterable: {checkAll: true, multi: true, search: true, dataSource: B_Date}
        // },
        // {
        //     field: "client_code",
        //     title: '客户单号',
        //     width: 110,
        //     attributes: {'class': 'kendo-custum-char'},
        //     headerAttributes: {'style': 'text-align:center;'},
        //     footerAttributes: {'style': 'color: red;text-align:center;'},
        //     filterable: false
        //     // filterable: {checkAll: true, multi: true, search: true, dataSource: B_Date}
        // },
        {
            field: "product_name",
            title: '品名',
            width: 110,
            attributes: {'class': 'kendo-custum-char'},
            headerAttributes: {'style': 'text-align:center;'},
            footerAttributes: {'style': 'color: red;text-align:center;'},
            // filterable: false
            filterable: {checkAll: true, multi: true, search: true, dataSource: product_name}
        },
        {
            field: "greyfabric_width",
            title: '门幅',
            width: 110,
            attributes: {'class': 'kendo-custum-char'},
            headerAttributes: {'style': 'text-align:center;'},
            footerAttributes: {'style': 'color: red;text-align:center;'},
            // filterable: false
            filterable: {checkAll: true, multi: true, search: true, dataSource: greyfabric_width}
        },
        {
            field: "material_specification",
            title: '克重',
            width: 110,
            attributes: {'class': 'kendo-custum-char'},
            headerAttributes: {'style': 'text-align:center;'},
            footerAttributes: {'style': 'color: red;text-align:center;'},
            // filterable: false
            filterable: {checkAll: true, multi: true, search: true, dataSource: material_specification}
        },
        {
            field: "sparei01",
            title: '克重差',
            width: 80,
            template: item => item['sparei01'] ? parseFloat(item['sparei01']).toFixed(0) : '',
            attributes: {'class': 'kendo-custum-char'},
            headerAttributes: {'style': 'text-align:center;'},
            footerAttributes: {'style': 'color: red;text-align:center;'},
            filterable: false
            // filterable: {checkAll: true, multi: true, search: true, dataSource: material_specification}
        },
        {
            field: "mkz",
            title: '米克重',
            width: 90,
            template: item => item['mkz'] ? parseFloat(item['mkz']).toFixed(2) : '',
            // footerTemplate: '#=kendo.format("{0:n2}",sum)#',
            footerAttributes: {'style': 'text-align:right;'},
            attributes: {'class': 'kendo-custum-number'},
            headerAttributes: {'style': 'text-align:center;'},
            filterable: false
        },
        {
            field: "B_Date",
            title: '打卷时间',
            width: 110,
            attributes: {'class': 'kendo-custum-char'},
            headerAttributes: {'style': 'text-align:center;'},
            footerAttributes: {'style': 'color: red;text-align:center;'},
            filterable: false
            // filterable: {checkAll: true, multi: true, search: true, dataSource: B_Date}
        },
        {
            field: "color",
            title: '颜色',
            width: 110,
            attributes: {'class': 'kendo-custum-char'},
            headerAttributes: {'style': 'text-align:center;'},
            footerAttributes: {'style': 'color: red;text-align:center;'},
            // filterable: false
            filterable: {checkAll: true, multi: true, search: true, dataSource: color}
        },
        // {
        //     field: "pssum",
        //     title: '匹数',
        //     width: 60,
        //     template: item => item['pssum'] ? parseFloat(item['pssum']).toFixed(0) : '',
        //     footerTemplate: '#=kendo.format("{0:n0}",sum)#',
        //     footerAttributes: {'style': 'text-align:right;'},
        //     attributes: {'class': 'kendo-custum-number'},
        //     headerAttributes: {'style': 'text-align:center;'},
        //     filterable: false
        // },
        {
            field: "B_MS",
            title: '米数',
            width: 90,
            template: item => item['B_MS'] ? parseFloat(item['B_MS']).toFixed(1) : '',
            footerTemplate: '#=kendo.format("{0:n1}",sum)#',
            footerAttributes: {'style': 'text-align:right;'},
            attributes: {'class': 'kendo-custum-number'},
            headerAttributes: {'style': 'text-align:center;'},
            filterable: false
        },
        {
            field: "qtysum",
            title: '重量',
            width: 90,
            template: item => item['qtysum'] ? parseFloat(item['qtysum']).toFixed(1) : '',
            footerTemplate: '#=kendo.format("{0:n1}",sum)#',
            footerAttributes: {'style': 'text-align:right;'},
            attributes: {'class': 'kendo-custum-number'},
            headerAttributes: {'style': 'text-align:center;'},
            filterable: false
        },
        {
            field: "isoverstep_kz",
            title: '超克重',
            width: 70,
            attributes: {'class': 'kendo-custum-char'},
            headerAttributes: {'style': 'text-align:center;'},
            footerAttributes: {'style': 'color: red;text-align:center;'},
            // filterable: false
            filterable: {checkAll: true, multi: true, search: true, dataSource: isoverstep_kz}
        },
        {
            field: "memo",
            title: ' ',
            width: 50,
            filterable: false
        },
    ];
    let grid = $("#v_grid_detail_grammage").kendoGrid({
        sortable: true,
        resizable: true,
        selectable: "multiple row",
        editable: false,
        filterable: {
            extra: false
        },
        dataSource: dataSource,
        // columns:columns,
        columnMenu: false,
        excel: {
            fileName: "克重表明细.xlsx",
            proxyURL: "",
            allPages: true,
            filterable: true
        },
        height: '99%',
        // pageable: {
        //     pageSize: 200
        // },
        reorderable: true,
        dataBound: function () {
            let rows = this.items();
            let kGrid = $('#v_grid_detail_grammage').data('kendoGrid');
            $(rows).each(function () {
                let index = $(this).index() + 1;
                let rowLabel = $(this).find(".row_number");
                $(rowLabel).html(index);
                let dataItem = kGrid.dataItem($(this));
            });

            let trs = kGrid.tbody.find("tr");
            trs.each(function (i, row) {
                let data = kGrid.dataItem(row);
                let isoverstep_kz = data['isoverstep_kz'];
                // if (isoverstep_kz == '超') $(row).css({"color": "red", "text-decoration": "line-through"});
                if (isoverstep_kz == '超') $(row).css({"color": "red"});
            });

        },
        columnHide: function (e) {
            //列隐藏事件
            let field = e.column.field;
            columnResizeEvent($('body').data('menu_id'), field, null, true, null, SUFFIX);
        },
        columnShow: function (e) {
            //列显示事件
            let field = e.column.field;
            columnResizeEvent($('body').data('menu_id'), field, null, false, null, SUFFIX);
        },
        columnResize: function (e) {
            //  列伸缩事件
            let field = e.column.field;
            let newWidth = e.newWidth;
            if (newWidth < 20) {
                columnResizeEvent($('body').data('menu_id'), field, null, true, null, SUFFIX);
            } else {
                columnResizeEvent($('body').data('menu_id'), field, newWidth, null, null, SUFFIX);
            }
        },
        columnReorder: function (e) {
            //  列拖动事件
            let newColumns = JSON.parse(JSON.stringify(e.sender.columns))
            newColumns = reorder(newColumns, e.oldIndex, e.newIndex);
            let menuId = $('body').data('menu_id');
            let col = []
            for (let i = 0; i < newColumns.length; i++) {
                if (newColumns[i]['field'])
                    col.push({
                        serialNum: i,
                        sysMenuId: menuId,
                        field: newColumns[i]['field'] + SUFFIX
                    });
            }
            $.ajax({
                url: 'sysUserTableConfig/modify/reorder',
                contentType: 'application/json',
                data: JSON.stringify(col),
                type: 'post',
                dataType: 'json',
                success: function (data) {
                }
            })
        },
    });
    let grid01 = $('#v_grid_detail_grammage').data('kendoGrid');
    loadOptions(grid01, columns, SUFFIX, $('body').data('menu_id'));
    grid01.setOptions({
        columns: columns
    });
    grid.on('dblclick', 'tr', function (e) {
        // setgrid_fh();
        // //  修改单据
        // let kGrid = $("#v_grid_detail_grammage").data("kendoGrid");
        // let dataItem = kGrid.dataItem(e.currentTarget);
        // open_fhdetail(dataItem);
    })
    grid.on('click', 'tr', function (e) {
        let kGrid = $("#v_grid_detail_grammage").data("kendoGrid");
        let dataItem = kGrid.dataItem(e.currentTarget);
        console.log(dataItem);
        // if (dataItem['is_audit']) {
        //     $('#btn_cancel_audit').linkbutton('enable')
        // } else {
        //     $('#btn_cancel_audit').linkbutton('disable')
        // }
    })
}

/************************************************************
 * */

//打印发货单
function btn_print_bill() {
    let reportlets = [];
    $.get('customize/getDatabase', {}, data => {
        if (data.success) {
            switch (getCurrentIndex()) {
                case 0:
                    break;
                case 1:
                    let kGrid = $('#v_grid_0').data('kendoGrid');
                    var selectedRows = kGrid.select();
                    for (let i = 0; i < selectedRows.length; i++) {
                        let dataItem = kGrid.dataItem(selectedRows[i]);
                        let Obj= {
                            reportlet: `${data.data}/invoice-cpfh.cpt`,
                            id: dataItem.id
                        };
                        reportlets.push(Obj);
                    }
                    console.log(reportlets);
                    window.parent.FR.doURLPDFPrint({
                        url: $(window.parent.document.body).data('report_url'),
                        isPopUp: false,
                        data: {
                            reportlets: reportlets
                        }
                    })
                    break;
                case 2:
                    break;
            }
        }
    }, 'json');
}

//库存明细获取数据
function getckdetail(m_FPDID,m_barcode, dataItem) {
    $.messager.progress({title: '', msg: '请稍等...', interval: 5000});
    let index = getCurrentIndex();
    if (index == 1) {
        $.ajax({
            url: "cpBillfh/listQuery",
            type: 'GET',
            data: {mainId: dataItem.id},
            success: function (cdata) {
                if (cdata.length) {
                    $('#fh_dg_bill').datagrid('clearSelections');
                    $('#fh_dg_bill').datagrid('loadData', cdata);
                }
            }, complete: function () {
                $.messager.progress('close');
            }
        })
    } else {
        $.ajax({
            url: "customize/callProc",
            type: 'post',
            data: JSON.stringify(['usp_getcpjrkdetail',m_FPDID,m_barcode]),
            contentType: "application/json;charset=UTF-8",
            success: function (cdata) {
                let  data=cdata.data;
                if (Number(m_FPDID)>0){
                    $('#fh_dg').datagrid('clearSelections');
                    $('#fh_dg').datagrid('loadData', data);
                }else{
                    $('#ck_dg').datagrid('clearSelections');
                    $('#ck_dg').datagrid('loadData', data);
                }
            }, complete: function () {
                $.messager.progress('close');
            }
        })
    }
}
//清除过滤
function btn_cancel_filter() {
    switch (getCurrentIndex()) {
        case 0:
            $('#v_grid_detail').data('kendoGrid').dataSource.filter({});
            break;
        case 1:
            $('#v_grid_2').data('kendoGrid').dataSource.filter({});
            break;
        case 2:
            $('#v_grid_detail_grammage').data('kendoGrid').dataSource.filter({});
            break;
    }
}
//查询事件
function btn_search() {
    switch (getCurrentIndex()) {
        case 0:
            getgriddata(0);
            break;
        case 1:
            getgriddata(2);
            break;
        case 2:
            getgriddata(3);
            break;
    }
}

/********************************************
 */
//获取标签页序号
function getCurrentIndex() {
    let tab = $('#tabs').tabs('getSelected');
    let index = $('#tabs').tabs('getTabIndex', tab);
    return index;
}
//获取当前标签页的网格对象
function getSelectedGrid() {
    let kendoGridID = '';
    switch (getCurrentIndex()) {
        case 0:
            kendoGridID = '#v_grid_sum';

            break;
        case 1:
            kendoGridID = '#v_grid_2';
            break;
        case 2:
            kendoGridID = '#v_grid_sum_grammage';
            break;
    }
    return $(kendoGridID).data('kendoGrid');
}
//打印
function btn_print() {
    let reportlets = [];
    $.get('customize/getDatabase', {}, data => {
        if (data.success) {
            switch (getCurrentIndex()) {
                case 0:
                    let kGrid = $('#v_grid').data('kendoGrid');
                    var selectedRows = kGrid.select();
                    for (let i = 0; i < selectedRows.length; i++) {
                        let dataItem = kGrid.dataItem(selectedRows[i]);
                        if (dataItem.client){
                            let Obj= {
                                reportlet: `${data.data}/invoice-fhd.cpt`,
                                id: dataItem.id,
                                database: data.data
                            };
                            reportlets.push(Obj);
                        }else{
                            $.messager.alert({title: '提示', msg: "未设置客户，不允许打印！"});
                            return;
                        }

                    }
                    console.log(reportlets);
                    window.parent.FR.doURLPDFPrint({
                        url: $(window.parent.document.body).data('report_url'),
                        isPopUp: false,
                        data: {
                            reportlets: reportlets
                        }
                    })
                    break;
                case 1:
                    let datagrid = $('#fh_dg_bill').datagrid('getSelections');
                    if (!datagrid.length) {
                        $.messager.alert({title: '提示', msg: "请勾选行后再试！"});
                        return;
                    }
                    for (let i = 0; i < datagrid.length; i++) {
                        let dataItem = datagrid[i];
                        if (dataItem.client){
                            let Obj= {
                                reportlet: `${data.data}/invoice-fhd.cpt`,
                                id: dataItem.id,
                                database: data.data
                            };
                            reportlets.push(Obj);
                        }else{
                            $.messager.alert({title: '提示', msg: "未设置客户，不允许打印！"});
                            return;
                        }

                    }
                    console.log(reportlets);
                    window.parent.FR.doURLPDFPrint({
                        url: $(window.parent.document.body).data('report_url'),
                        isPopUp: false,
                        data: {
                            reportlets: reportlets
                        }
                    })
                    break;
                case 2:
                    // reportlets.push({
                    //     reportlet: `${data.data}/色布库存序时表打印.cpt`,
                    //     Sdate: $('#start').datebox('getValue'),
                    //     Edate: $('#end').datebox('getValue'),
                    //     clientName: $('#contactCompanyId').combobox('getValue') || '',
                    //     database: data.data
                    // });
                    // window.parent.FR.doURLPDFPrint({
                    //     url: $(window.parent.document.body).data('report_url'),
                    //     isPopUp: false,
                    //     data: {
                    //         reportlets: reportlets
                    //     }
                    // })
                    break;
            }
        }
    }, 'json');
}

//z自动列设置
function btn_open_column_list() {
    let kendoGridID = '';
    switch (getCurrentIndex()) {
        case 0:
            kendoGridID = '#v_grid_detail';
            break;
        case 1:
            kendoGridID = '#v_grid_2';
            break;
        case 2:
            kendoGridID = '#v_grid_detail_grammage';
            break;
    }
    let options = $(kendoGridID).data('kendoGrid').getOptions().columns;
    if ($('#btn_open_column_list').length <= 0) {
        let div = $(`<div data-options="resizable:true,iconCls:'icon-text_columns',width:200,height:300,modal:true,title:'列',
                                         onClose:function(){$(this).dialog('destroy')}   " 
                          class="easyui-dialog" id="btn_open_column_list">
                   </div>`);
        let form = $('<form style="width: 100%;height: 100%;"></form>')
        for (let i of options) {
            if (i.field) {
                let checkbox = $(`<div style="width: 100%;height: auto;padding: 5px;">
                <input class="easyui-checkbox" name="${i.field}" data-options="label:'${i.title}',
                labelWidth:120,value:true,name:'${i.field}'">
                </div>`);
                form.append(checkbox);
            }
        }
        let container = $(`<div></div>`);
        form.appendTo(div);
        div.appendTo(container);
        $('body').append(container);
        $.parser.parse($('#btn_open_column_list').parent());
        let formData = {};
        options.map(item => {
            formData[item['field']] = item['hidden'] == undefined ? true : !item['hidden'];
        })
        $('#btn_open_column_list form').eq(0).form('load', formData);
        $('#btn_open_column_list form .easyui-checkbox').each(function () {
            //  绑定事件
            $(this).checkbox({
                onChange: function (checked) {
                    let opts = $(this).checkbox('options');
                    if (checked) {
                        $(kendoGridID).data('kendoGrid').showColumn(opts.name);
                    } else {
                        $(kendoGridID).data('kendoGrid').hideColumn(opts.name);
                    }
                }
            })
        })
    } else {
        $('#btn_open_column_list').dialog('open');
    }
}
//导出
function btn_export_excel() {
    switch (getCurrentIndex()) {
        case 0:
            $('#v_grid_sum').data('kendoGrid').saveAsExcel();
            $('#v_grid_detail').data('kendoGrid').saveAsExcel();
            break;
        case 1:
            $('#v_grid_2').data('kendoGrid').saveAsExcel();
            break;
        case 2:
            $('#v_grid_sum_grammage').data('kendoGrid').saveAsExcel();
            $('#v_grid_detail_grammage').data('kendoGrid').saveAsExcel();
            break;
    }
}


function btn_close() {
    window.parent.closeTab();
}
